using System.Text.RegularExpressions;

namespace Gateway.Common.Extensions;

/// <summary>
/// 正则扩展
/// </summary>
public static class RegexExtension
{
    /// <summary>
    /// 密码不符合规则提示
    /// </summary>
    private const string PasswordFailMessage = "密码无效，密码规则：至少8位最多16位，包含大写字母、小写字母、数字、特殊字符中至少3种组合";

    /// <summary>
    /// 检查密码
    /// </summary>
    /// <param name="password"></param>
    /// <returns></returns>
    public static (bool success, string message) CheckPassword(this string password)
    {
        if (string.IsNullOrEmpty(password))
        {
            return (false, "密码不能为空");
        }

        var regex =
            @"^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_]+$)(?![a-z0-9]+$)(?![a-z\W_]+$)(?![0-9\W_]+$)[a-zA-Z0-9\W_]{8,16}$";
        var verify = Regex.Match(password, regex);
        if (!verify.Success)
        {
            return (false, PasswordFailMessage);
        }

        return (true, "");
    }

    /// <summary>
    /// 检查手机号格式
    /// </summary>
    /// <param name="phone"></param>
    /// <returns></returns>
    public static (bool success, string message) CheckPhone(this string phone)
    {
        if (string.IsNullOrEmpty(phone))
        {
            return (false, "手机号不能为空");
        }

        var regex =
            @"^1(3|4|5|6|7|8|9)\d{9}$";
        var verify = Regex.Match(phone, regex);
        if (!verify.Success)
        {
            return (false, "手机号格式不正确");
        }

        return (true, "");
    }
}